﻿using System;
using System.Text;
using Newtonsoft.Json;

namespace Hsenl {
    public class DBSerializeJson : IDBSerialize {
        private readonly JsonSerializerSettings _settings = new() {
            Formatting = Formatting.Indented,
            ReferenceLoopHandling = ReferenceLoopHandling.Ignore,
            TypeNameHandling = TypeNameHandling.Auto
        };

        public byte[] Serialize(object obj) {
            var json = SerializeHelper.SerializeOfJson(obj, this._settings);
            var bytes = Encoding.UTF8.GetBytes(json);
            return bytes;
        }

        public byte[] Serialize<T>(T t) {
            var json = SerializeHelper.SerializeOfJson(t, this._settings);
            var bytes = Encoding.UTF8.GetBytes(json);
            return bytes;
        }

        public T Deserialize<T>(byte[] data) {
            var json = Encoding.UTF8.GetString(data);
            return SerializeHelper.DeserializeOfJson<T>(json, this._settings);
        }

        public object Deserialize(byte[] data, Type type) {
            var json = Encoding.UTF8.GetString(data);
            return SerializeHelper.DeserializeOfJson(json, type, this._settings);
        }
    }
}